package com.xhorse.lockplatform.comm.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author Tom
 */
public class DateUtils {

    private DateUtils() {
    }

    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String SIMPLE_DATE_PATTERN = "yyyy/MM/dd";
    public static final String DEFAULT_PATTERN_TIME = "HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String FILENAME_DATE_PATTERN = "yyMMddHHmmssSSS";
    public static final String PAYMENT_DATE_PATTERN = "yyyyMMddHHmmss";
    public static final String CN_DATE_PATTERN = "yyyy年MM月dd日 HH时mm分";

    public static String nowDateString(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 获取文件名前缀，以日期为格式
     *
     * @return 171122201923448
     */
    public static String getFilePrefix() {
        return nowDateString(FILENAME_DATE_PATTERN);
    }

    public static String getPaymentDate() {
        return nowDateString(PAYMENT_DATE_PATTERN);
    }

    public static String dateStringAboutNow(String pattern, int dateInterval) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, dateInterval);
        return DateFormatUtils.format(cal.getTime(), pattern);
    }

    public static String yesterdayString(String pattern) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        return DateFormatUtils.format(cal.getTime(), pattern);
    }

    public static Date parse(String date, Date defDate) {
        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(date, DEFAULT_PATTERN);
        } catch (ParseException e) {
            return defDate;
        }
    }

    public static Date parse(String date) throws IllegalArgumentException {
        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(date, DEFAULT_PATTERN);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * 获取输入字串表示的时间的前一天的结束时间和第二天的开始时间
     *
     * @param time
     * @return
     * @throws IllegalArgumentException
     */
    public static Tuple<Date, Date> dateInterval(String time) throws ParseException {
        Date thisTime = org.apache.commons.lang3.time.DateUtils.parseDate(time, SIMPLE_DATE_PATTERN);
        Calendar cal = Calendar.getInstance();
        cal.setTime(thisTime);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MILLISECOND, -1);
        Date beginTime = cal.getTime();
        cal.add(Calendar.MILLISECOND, 1000 * 60 * 60 * 24 + 1);
        Date endTime = cal.getTime();
        return Tuple.valueOf(beginTime, endTime);
    }

    public static Calendar beginningOfTheDate() {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    public static String formatNowToMinute() {
        return new SimpleDateFormat(DEFAULT_PATTERN_TIME).format(new Date());
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 毫秒数
     * @return
     */
    public static String timeStamp2Date(long seconds) {
        String format = "yyyy-MM-dd";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new Date(seconds));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 毫秒转化时分秒毫秒
     *
     * @param ms
     * @return
     */
    public static String timeStamp2Days(long ms) {
        String flag = "";
        if (ms < 0) {
            flag = "-";
        }
        long absMs = Math.abs(ms);

        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = absMs / dd;
        Long hour = (absMs - day * dd) / hh;
        Long minute = (absMs - day * dd - hour * hh) / mi;
        Long second = (absMs - day * dd - hour * hh - minute * mi) / ss;

        StringBuilder sb = new StringBuilder();
        sb.append(flag);
        if (day > 0) {
            sb.append(day).append("d ");
        }
        if (hour > 0) {
            sb.append(hour).append("h ");
        }
        if (minute > 0) {
            sb.append(minute).append("m ");
        }
        if (second > 0) {
            sb.append(second).append("s");
        }
        return sb.toString();
    }

    /**
     * 格式 yyyy-MM-dd
     */
    public static String toDateStr(Calendar calendar) {
        return dateToString(calendar, DATE_PATTERN);
    }

    /**
     * 格式 yyyy-MM-dd
     */
    public static String toDateStr(Date date) {
        return dateToString(date, DATE_PATTERN);
    }

    /**
     * 获取前一天日期
     */
    public static String getPreTodayDate(Calendar calendar) {
        Calendar newEndValidFor = (Calendar) calendar.clone();
        newEndValidFor.add(Calendar.DATE, -1);
        return toDateStr(newEndValidFor);
    }

    /**
     * 获取前一天日期
     */
    public static String getPreXDate(Calendar calendar, int x) {
        Calendar newEndValidFor = (Calendar) calendar.clone();
        newEndValidFor.add(Calendar.DATE, x);
        return toDateStr(newEndValidFor);
    }

    /**
     * 时间转化
     *
     * @param calendar 时间
     * @param pattern  传入匹配模式
     * @return 格式化后的时间字符串
     */
    public static String dateToString(Calendar calendar, String pattern) {
        if (calendar == null) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(calendar.getTime());
    }

    public static String dateToString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static String longToString(long currentTime, String pattern)
            throws ParseException {
        // long类型转成Date类型
        Date date = longToDate(currentTime, pattern);
        return dateToString(date, pattern);
    }

    public static String longToString(long currentTime) throws ParseException {
        return longToString(currentTime, DEFAULT_PATTERN);
    }

    public static Date stringToDate(String strTime, String pattern)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date date;
        date = formatter.parse(strTime);
        return date;
    }

    public static Date longToDate(long currentTime, String pattern)
            throws ParseException {
        // 根据long类型的毫秒数生命一个date类型的时间
        Date dateOld = new Date(currentTime);
        // 把date类型的时间转换为string
        String sDateTime = dateToString(dateOld, pattern);
        return stringToDate(sDateTime, pattern);
    }

    public static Date longToDate(long currentTime) throws ParseException {
        return longToDate(currentTime, DEFAULT_PATTERN);
    }

    public static long stringToLong(String strTime, String pattern)
            throws ParseException {
        // String类型转成date类型
        Date date = stringToDate(strTime, pattern);
        if (date == null) {
            return 0;
        } else {
            return dateToLong(date);
        }
    }

    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * 获取未来 第 past 天的日期
     *
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        LocalDate today = LocalDate.now();
        return today.plusDays(past).toString();
    }

    public static void main(String[] args) {
        System.out.println(getFetureDate(7));
    }
}
